package components;

import GUInemeanshop.BasketPanel;
import java.util.ArrayList;
import java.util.List;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import nemeanshop.Product;

/**
 *
 * @author Ragnar
 */
public class NTablemod2 extends AbstractTableModel
{
    private List<Product> products;
    private BasketPanel bp;
    
    public NTablemod2(List<Product> prods, BasketPanel bp)
    {
        this.products = prods;
        this.bp = bp;
        
    }

    @Override
    public Class<?> getColumnClass(int columnIndex)
    {
        return Product.class;
    }

    @Override
    public String getColumnName(int column)
    {
        return "Produkt";
    }

    @Override
    public int getColumnCount()
    {
        return 1;
    }

    @Override
    public int getRowCount()
    {
        return this.products.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex)
    {
        return (this.products == null) ? null : this.products.get(rowIndex);
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex)
    {
        return true;
    }   
    
    public double getTotal()
    {
        double tot = 0;
        for (Product p : this.products)
        {
            tot += (p.getTotal()*p.getSellPrice());
        }
        return tot;
    }
    
    public void update(List<Product> p)
    {
        int index = products.size();
        products = p;
        this.fireTableChanged(new TableModelEvent(this, 0, index, -1, TableModelEvent.ALL_COLUMNS));
    }

    public void removeRow(int index)
    {
        //products.remove(index);
        int index2 = products.size();
        this.fireTableRowsDeleted(index, index);
    }
    
    public void reset()
    {
        int index = products.size();
        products = new ArrayList<Product>();
        this.fireTableChanged(new TableModelEvent(this, 0, index, -1, TableModelEvent.DELETE));
    }
    
    public void updateBP()
    {
        bp.updateOrder();
    }
    
}
